﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoursePack
{
    [Serializable]
    public class CourseDescription
    {
        #region Datat
        protected string m_CourseName;
        protected string m_CourseNumber;
        protected string m_CourseTitle;
        protected float m_Credits;
        protected string m_Grade;

        protected string m_strPrerequisites;
        protected string m_SpecialNotes;

        protected List<Prerequisite> m_Prerequisites;
        protected CourseStatus m_CourseStatus;

        #endregion
        #region Constructor

        public CourseDescription(string _CourseName, string _CourseNumber, string _CourseTitle, string _Credits, string _Prereq, string _SpecialNotes,CourseStatus _Status)
        {
            this.m_CourseName = _CourseName;
            this.m_CourseNumber = _CourseNumber;
            this.m_CourseTitle = _CourseTitle;
            
            
            this.m_Credits = Tools.ParseCredits(_Credits);

            this.m_strPrerequisites = _Prereq;
            this.m_Prerequisites = new List<Prerequisite>();
            filterPrereq(_Prereq);

            this.m_SpecialNotes = _SpecialNotes;

            this.m_CourseStatus = _Status;
        }
        #endregion

        private void filterPrereq(string _Courses)
        {
            string[] prereqs = _Courses.Split(';');


            foreach (string str in prereqs)
            {
                if (str == "")
                    continue;

                Prerequisite pre = new Prerequisite();
                string[] prereq = str.Split(new string[] { " or " }, StringSplitOptions.None);


                foreach (string str2 in prereq)
                {

                    if (!str2.Contains(','))
                    {
                        string[] course = Tools.filterCourse(str2);
                        string coursename = course[0];
                        string courseNumber = course[1];

                        CourseDescription c = new CourseDescription(coursename, courseNumber, "", "0","","",CourseStatus.Completed);
                        pre.AddCourse(c);

                    }
                    else
                    {
                        string[] prere = str2.Split(',');

                        foreach (string str3 in prere)
                        {
                            string[] course = Tools.filterCourse(str3);
                            string coursename = course[0];
                            string courseNumber = course[1];

                            CourseDescription c = new CourseDescription(coursename, courseNumber, "", "0","","",CourseStatus.Completed);
                            pre.AddCourse(c);
                        }
                    }
                }
                this.m_Prerequisites.Add(pre);
            }
        }

        public override string ToString()
        {
            return this.m_CourseName + " " + this.m_CourseNumber + " ";
        }

        #region Accessors
        public bool IsCompleted
        {
            get { return this.m_CourseStatus == CourseStatus.Completed; }
        }

        public string CourseName
        {
            get { return this.m_CourseName; }
        }

        public string CourseNumber
        {
            get { return this.m_CourseNumber; }
        }

        public string CourseTitle
        {
            get { return this.m_CourseTitle; }
        }

        public float Credits
        {
            get { return this.m_Credits; }
        }

        public string Grade
        {
            get
            {
                if (this.m_CourseStatus == CourseStatus.Completed)
                    return this.m_Grade;
                else
                    return this.m_CourseStatus.ToString();
            }
            set
            {
                if (this.m_CourseStatus == CourseStatus.Completed)
                    this.m_Grade = value;
            }
        }

        #endregion

    }
}
